/***********************************************************************
 * Dao Interface :  SysRoleDao.java
 * Author:  Bill Vison
 * Purpose: Defines the Hibernate Implement SysRoleDaoImpl
 ***********************************************************************/
package org.vsg.security.model.persistence.hibernate;

import java.io.Serializable;
import java.util.Collection;
import java.util.Iterator;
import java.util.Vector;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.vsg.common.pagination.Pagination;
import org.vsg.common.pagination.PaginationException;
import org.vsg.common.pagination.dao.PaginationHibernateDaoSupport;
import org.vsg.common.pagination.datatrans.PaginationProvider;
import org.vsg.security.model.domain.SysRole;
import org.vsg.security.model.persistence.SysRoleDao;
import org.vsg.security.model.persistence.hibernate.proc.AvailableConflictRolePaginationProc;
import org.vsg.security.model.persistence.hibernate.proc.AvailableHierarchyRolePaginationProc;
import org.vsg.security.model.persistence.hibernate.proc.SysRoleByAllPaginationProc;
import org.vsg.security.model.persistence.hibernate.proc.SysRoleHierarchyPaginationProc;
import org.vsg.security.model.persistence.hibernate.proc.SysRoleOfUserPaginationProc;
import org.vsg.security.model.persistence.hibernate.proc.SysRolePaginationProc;
import org.vsg.security.model.persistence.hibernate.proc.UndefinedHierarchyRolePaginationProc;

/**
 * @author Bill Vison
 */
public class SysRoleDaoImpl extends PaginationHibernateDaoSupport implements SysRoleDao {

	/**
	 * set the logger for develope code , do not use System.print(ln) to out print message.
	 */
	private static final Logger logger = LoggerFactory.getLogger(SysRoleDaoImpl.class);

	
	/**
	 *
	 * @param sysRole
	 * @exception DataAccessException
	 * @see org.vsg.security.model.persistence.SysRoleDao#save(org.vsg.security.model.domain.SysRole)
	 */
	@Override
	public void save(SysRole sysRole) throws DataAccessException {
		// TODO Auto-generated method stub
		getHibernateTemplate().save(sysRole);
	}

	@Override
	public void saveAll(Collection<SysRole> all)
			throws DataAccessException {
		// TODO Auto-generated method stub
		
		getHibernateTemplate().saveOrUpdateAll(all);
		
	}   	
   	
	/**
	 *
	 * @param sysRole
	 * @exception DataAccessException
	 * @see org.vsg.security.model.persistence.SysRoleDao#update(org.vsg.security.model.domain.SysRole)
	 */
   	@Override
   	public void update(SysRole sysRole) throws DataAccessException {
   		// TODO Auto-generated method stub
		getHibernateTemplate().update(sysRole);
	}   	


	/**
	 *
	 * @param sysRole
	 * @exception DataAccessException
	 * @see org.vsg.security.model.persistence.SysRoleDao#delete(org.vsg.security.model.domain.SysRole)
	 */
	@Override
	public void delete(SysRole sysRole) throws DataAccessException {
		// TODO Auto-generated method stub
		getHibernateTemplate().delete(sysRole);
	}

	@Override
	public void deleteAll(Collection<SysRole> all)
			throws DataAccessException {
		// TODO Auto-generated method stub
		getHibernateTemplate().deleteAll(all);
	}
   	
   	
	/**
	 *
	 * @param pk , the primary key
	 * @exception DataAccessException
	 * @see org.vsg.security.model.persistence.SysRoleDao#load(java.io.Serializable)
	 */
	@Override
	public SysRole load(Serializable pk) throws DataAccessException {
		// Start of user code TODO Auto-generated method stub
		SysRole sysRole = (SysRole) getHibernateTemplate().load(SysRole.class, pk);
		
		return sysRole;	
		// End of user code	
	}
   
   
	/**
	 *
	 * @param provider
	 * @exception DataAccessException
	 * @exception PaginationException
	 * @see org.vsg.security.model.persistence.SysRoleDao#pageSysRole(org.vsg.common.pagination.datatrans.PaginationProvider)
	 */
	@Override
	public Pagination pageSysRole(PaginationProvider provider) throws DataAccessException, PaginationException {
		// Start of user code TODO Auto-generated method stub
		SysRolePaginationProc proc = new SysRolePaginationProc();
		
		Pagination page = generatePagination(proc , provider);
		
		return page;
		// End of user code	
	}      	

	
	@Override
	public Pagination pageAvailableRolesOfUser(PaginationProvider provider)
			throws DataAccessException, PaginationException {
		// TODO Auto-generated method stub
		SysRoleOfUserPaginationProc proc = new SysRoleOfUserPaginationProc();
		
		Pagination page = generatePagination(proc , provider);
		
		return page;
	}
	
	

	@Override
	public Pagination pageUndefinedHierarchyRoles(PaginationProvider provider)
			throws DataAccessException, PaginationException {
		// TODO Auto-generated method stub
		UndefinedHierarchyRolePaginationProc proc = new UndefinedHierarchyRolePaginationProc();
		Pagination page = this.generatePagination(proc, provider);
		return page;

	}

	@Override
	public Pagination pageSysRoleHierarchy(PaginationProvider provider)
			throws DataAccessException, PaginationException {
		// TODO Auto-generated method stub
		SysRoleHierarchyPaginationProc proc = new SysRoleHierarchyPaginationProc();
		Pagination page = generatePagination(proc , provider);
		return page;
	}

	@Override
	public Pagination pageAvailableHierarchyRoles(PaginationProvider provider)
			throws DataAccessException, PaginationException {
		// TODO Auto-generated method stub
		AvailableHierarchyRolePaginationProc proc = new AvailableHierarchyRolePaginationProc();
		Pagination page = generatePagination(proc , provider);
		return page;
	}
	

	@Override
	public Pagination pageAvailableConflictRole(PaginationProvider provider)
			throws DataAccessException, PaginationException {
		// TODO Auto-generated method stub
		AvailableConflictRolePaginationProc proc = new AvailableConflictRolePaginationProc();
		Pagination page = generatePagination(proc , provider);
		return page;
	}

	/**
	 *
	 * @param provider
	 * @exception DataAccessException
	 * @exception PaginationException
	 * @see org.vsg.security.model.persistence.SysRoleDao#pageSysRoleByAll(org.vsg.common.pagination.datatrans.PaginationProvider)
	 */
	@Override
	public Pagination pageSysRoleByAll(PaginationProvider provider) throws DataAccessException, PaginationException {
		// Start of user code TODO Auto-generated method stub
		SysRoleByAllPaginationProc proc = new SysRoleByAllPaginationProc();
		Pagination page = generatePagination(proc , provider);
		return page;
		// End of user code		
	}


	/**
	 * @exception DataAccessException
	 */
	@Override
	public Collection<SysRole> findByAll() throws DataAccessException {
		// Start of user code TODO Auto-generated method stub
		StringBuilder query = new StringBuilder("select ");

		query.append("sysRole ");
		query.append("from ").append(SysRole.class.getName()).append(" as sysRole ");
		query.append("where sysRole.flagDeleted = 0 ");
		query.append("order by sysRole.name");
		
		Collection params = new Vector();
		
		logger.info("query : " + query.toString());

		Iterator<SysRole> iter = getHibernateTemplate().iterate(query.toString());
		
		Collection<SysRole> result = new Vector<SysRole>();
		
		while (iter.hasNext()) {
			result.add( iter.next() );
		}
		
		return result;
		// End of user code			
	}

	@Override
	public Collection<SysRole> findByCode(String code)
			throws DataAccessException {
		// Start of user code TODO Auto-generated method stub
		StringBuilder query = new StringBuilder("select ");

		query.append("sysRole ");
		query.append("from ").append(SysRole.class.getName()).append(" as sysRole ");
		query.append("where sysRole.code = ? ");
		query.append("order by sysRole.name");
		
		Collection params = new Vector();
		params.add( code );
		
		logger.info("query : " + query.toString());

		Iterator<SysRole> iter = getHibernateTemplate().iterate(query.toString() , params);
		
		Collection<SysRole> result = new Vector<SysRole>();
		
		while (iter.hasNext()) {
			result.add( iter.next() );
		}
		
		return result;
		// End of user code			
	}

	@Override
	public Collection<SysRole> findParentsByRole(SysRole role)
			throws DataAccessException {
		// TODO Auto-generated method stub
		StringBuffer query = new StringBuffer("select ");
		query.append("sysRole ");
		query.append("from ").append(SysRole.class.getName()).append(" as sysRole ");
		query.append("inner join sysRole.dependenceRoles as dependenceRole ");
		query.append("where dependenceRole.id = ? ");		
		
		Collection params = new Vector();
		params.add(role.getId());
		
		Iterator<SysRole> iter = this.getHibernateTemplate().iterate(query.toString() , params.toArray());
		
		Collection<SysRole> sysRoles = new Vector<SysRole>();
		while (iter.hasNext()) {
			sysRoles.add(iter.next() );
		}
		return sysRoles;
	}

	
	
} 